PostgreSQL日志号LSN和wal日志文件简记

弄明白日志号的原理之后,一段时间又有点忘记了,干脆整理一遍:

 

(一)wal文件命名规则

1)在$PGDATA目录下面的pg_xlog目录中存放着xlog日志文件(10.1之后变为了pg_wal):

total 165896
-rw-r--r-- 1 apple staff 660K 11 22 15:46 000000010000000000000013.dump
-rw------- 1 apple staff 295B 11 22 16:12 000000010000000000000014.00000028.backup
-rw------- 1 apple staff 16M 12 15 16:28 000000010000000000000046
-rw------- 1 apple staff 16M 12 15 16:30 000000010000000000000047
-rw------- 1 apple staff 16M 12 15 16:30 000000010000000000000048
-rw-r--r-- 1 apple staff 357K 1 16 16:27 000000010000000000000044.dump
-rw------- 1 apple staff 16M 3 27 09:49 000000010000000000000045
-rw------- 1 apple staff 16M 3 27 09:49 000000010000000000000044
drwx------ 4 apple staff 136B 4 9 10:52 archive_status

2)wal日志号由64位组成,即有2**64个日志编号,用完了需要重置,但是这个数量已经很多了,相当于1024P个,1024*1024个T。

日志文件则由24个16进制数字组成,分三部分:时间线、LSN高32位、LSN低32位/(2**24)的值(即低32位中最高两位,表示该日志文件的LSN起始编号,结合具体的LSN方便理解一点)。

 

(二)在数据库查看具体的日志编号LSN

1)用到的一些方法:

apple=# select proname from pg_proc where proname like 'pg_%_location';
proname
---------------------------------
pg_current_xlog_insert_location
pg_current_xlog_location
pg_last_xlog_receive_location
pg_last_xlog_replay_location
pg_tablespace_location
(5 rows)

 

2)数据库中可以查询到当前的日志编号情况:

apple=# select pg_current_xlog_location();
pg_current_xlog_location
--------------------------
0/45000098
(1 row)

 

可以看到:

0表示LSN的高32位

45表示对应的xlog文件的最后两位

000098表示当前的LSN在对应的xlog中的偏移字节地址,我记得有人写过工具来读取xlog中对应块的数据并解析的,需要的时候再用用。

从current_xlog_location也可以推断出该日志对应的xlog文件:

时间线0000000000000045 ----> 000000010000000000000045

 

(三)LSN在用于恢复时

1)通过pg_controldata来获取control文件中的控制信息:

appledeMacBook-Pro-2:pg_xlog apple$ pg_controldata
pg_control version number: 942
Catalog version number: 201510051
Database system identifier: 6451705940496018968
Database cluster state: in production
pg_control last modified: 一 4/ 9 10:52:12 2018
Latest checkpoint location: 0/45000028        
Prior checkpoint location: 0/446C7878
Latest checkpoint's REDO location: 0/45000028  
Latest checkpoint's REDO WAL file: 000000010000000000000045
Latest checkpoint's TimeLineID: 1
Latest checkpoint's PrevTimeLineID: 1
Latest checkpoint's full_page_writes: on
Latest checkpoint's NextXID: 0/535519
Latest checkpoint's NextOID: 20278

 

2)用于恢复的两个LSN

Latest checkpoint location: 0/45000028         --REDO point is the location to write the XLOG record at the moment when the latest checkpoint is started, and is the starting point of database recovery
Prior checkpoint location: 0/446C7878
Latest checkpoint's REDO location: 0/45000028  --A XLOG record of this checkpoint (i.e. checkpoint record) is written to the WAL buffer. The data-portion of the record is defined by the structure CheckPoint, which contains several variables such as the REDO point

 

之前好像理解错了,应该是:

1、当执行checkpoint的时候,会立即写入一条xlog,但此时还在checkpoint 的进程中,就是redo location,作为之后恢复的lsn
2、当checkpoint操作这条记录,写入到wal_buffer的时候,会再次记录一个xlog,就是checkpoint location。

这两个点相差很小,至于checkpoint process中触发了checkpoint,到写入到wal buffer的间隔有多长,这块还要看一下

 

 

posted @ 2018-04-09 11:28  狂神314  阅读(6320)  评论(0编辑  收藏  举报